package com.jianyun.jyzs.utils;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.widget.ImageView;


import com.jianyun.jyzs.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtils {
	
	public static final int SCALE_IMAGE_WIDTH = 540+135;
	public static final int SCALE_IMAGE_HEIGHT = 960+240;


	public static String getScaledImage(String paramString){
		File localFile1 = new File(paramString);
		if (!localFile1.exists()){
			return paramString;
		}
		long l = localFile1.length();
		if (l <= 204800L){
			return paramString;
		}
		Bitmap localBitmap = decodeScaleImage(paramString, SCALE_IMAGE_WIDTH, SCALE_IMAGE_HEIGHT);
		try{
			File tempFile = new File(PathUtil.tempPathName);
			if(!tempFile.exists()){
				tempFile.mkdirs();
			}
			File localFile2 = File.createTempFile("image", ".jpg", tempFile);
	        int options = 90;  
			FileOutputStream localFileOutputStream = new FileOutputStream(localFile2);
			localBitmap.compress(CompressFormat.JPEG, 100, localFileOutputStream);
			localFileOutputStream.close();
			while (localFile2.length() > 204800L){
				localFileOutputStream = new FileOutputStream(localFile2);
				localBitmap.compress(CompressFormat.JPEG, options, localFileOutputStream);
				localFileOutputStream.close();
            	options -= 10;//每次都减少10  
			}
			return localFile2.getAbsolutePath();
		}catch (Exception localException){
			localException.printStackTrace();
		}
		return paramString;
	}
	public static Bitmap decodeScaleImage(String paramString, int width, int height){
		BitmapFactory.Options localOptions = getBitmapOptions(paramString);
		int i = calculateInSampleSize(localOptions, width, height);
		localOptions.inSampleSize = i;
		localOptions.inJustDecodeBounds = false;
		Bitmap localBitmap1 = BitmapFactory.decodeFile(paramString, localOptions);
		int j = readPictureDegree(paramString);
		Bitmap localBitmap2 = null;
		if ((localBitmap1 != null) && (j != 0)){
			localBitmap2 = rotaingImageView(j, localBitmap1);
			localBitmap1.recycle();
			localBitmap1 = null;
			return localBitmap2;
		}
		return localBitmap1;
	}
	
	public static Bitmap rotaingImageView(int paramInt, Bitmap paramBitmap){
	    Matrix localMatrix = new Matrix();
	    localMatrix.postRotate(paramInt);
	    Bitmap localBitmap = Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(), paramBitmap.getHeight(), localMatrix, true);
	    return localBitmap;
	}
	
	public static int readPictureDegree(String paramString){
	    int i = 0;
	    try{
	    	ExifInterface localExifInterface = new ExifInterface(paramString);
	    	int j = localExifInterface.getAttributeInt("Orientation", 1);
	    	switch (j){
	    		case 6:i = 90;break;
    			case 3:i = 180;break;
    			case 8:i = 270;break;
    			case 4:break;
    			case 5:break;
    			case 7:break;
	    	}
	    }catch (IOException localIOException){
	    	localIOException.printStackTrace();
	    }
	    return i;
	}
	
	public static int calculateInSampleSize(BitmapFactory.Options paramOptions, int paramInt1, int paramInt2){
	  
		int i = paramOptions.outHeight;
	    int j = paramOptions.outWidth;
	    int k = 1;
	    if ((i > paramInt2) || (j > paramInt1)){
	    	int l = Math.round(i / paramInt2);
	    	int i1 = Math.round(j / paramInt1);
	    	k = (l > i1) ? l : i1;
	    }
	    return k;
	}
	public static BitmapFactory.Options getBitmapOptions(String paramString){
	    BitmapFactory.Options localOptions = new BitmapFactory.Options();
	    localOptions.inJustDecodeBounds = true;
	    BitmapFactory.decodeFile(paramString, localOptions);
	    return localOptions;
	  }
	
	/**
     * 展示视频缩略图
     */
	public static  void showVideoThumbView(String videoPath, ImageView iv) {
    	if(videoPath == null || videoPath.equals("")){
            iv.setImageResource(R.drawable.app_panel_video_icon);
            return;
    	}
    	Bitmap bitmap = null;
    	File file = new File(PathUtil.imagePathName,FileUtil.getFileNoExtName(videoPath));
    	if(file != null && file.exists()){
    		iv.setImageBitmap(BitmapFactory.decodeFile(PathUtil.imagePathName+"/"+FileUtil.getFileNoExtName(videoPath)));
    		return;
    	}
    	FileOutputStream fos = null;
        try {
	    	if (!file.getParentFile().exists()) {
	            file.getParentFile().mkdirs();
	        }
            bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, 3);
            if (bitmap == null) {
                iv.setImageResource(R.drawable.app_panel_video_icon);
                return ;
            }
            fos = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        	 if (fos != null) {
                 try {
                     fos.close();
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
                 fos = null;
             }
            if (bitmap != null) {
                bitmap.recycle();
                bitmap = null;
            }
        }
        iv.setImageBitmap(BitmapFactory.decodeFile(PathUtil.imagePathName+"/"+FileUtil.getFileNoExtName(videoPath)));
    }
	public static Bitmap getVideoThumbnail(String filePath) {
		Bitmap bitmap = null;
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		try {
			retriever.setDataSource(filePath);
			bitmap = retriever.getFrameAtTime();
		}
		catch(IllegalArgumentException e) {
			e.printStackTrace();
		}
		catch (RuntimeException e) {
			e.printStackTrace();
		}
		finally {
			try {
				retriever.release();
			}
			catch (RuntimeException e) {
				e.printStackTrace();
			}
		}
		return bitmap;
	}
}
